package com.changgou.service.goods.controller;

import com.changgou.common.pojo.CustomException;
import com.changgou.common.pojo.PageResult;
import com.changgou.common.pojo.Result;
import com.changgou.common.pojo.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.changgou.service.goods.service.BrandService;
import com.github.pagehelper.Page;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: JoinCao
 * @CreateDate: 2020-03-13
 */
@RequestMapping("/brand")
@RestController
@Api(value = "品牌处理的controller", tags = "品牌接口")//一般不写value
//声明跨域请求的网站及请求方式
@CrossOrigin(origins = {"www.baidu.com", "www.heima.com"}, methods = {RequestMethod.GET, RequestMethod.POST})
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * @Author: JoinCao
     * @CreateDate: 2020-03-13
     * @Description: 品牌列表查询
     */
    @GetMapping
    public Result<List<Brand>> findList() {
        List<Brand> brandList = brandService.findList();
        return new Result<>(true, StatusCode.OK, "查询成功", brandList);
    }

    /**
     * @Author: JoinCao
     * @CreateDate: 2020-03-13
     * @Description: 根据id查询品牌数据
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询品牌", tags = "响应的结果是json")//一般不写tags
    @ApiImplicitParams({//传参文档描述
            @ApiImplicitParam(name = "id", value = "品牌的主键", required = true, dataType = "Integer",
                    defaultValue = "1115", paramType = "path")
    })
    @ApiResponses({//入参文档描述
            @ApiResponse(code = 20000, message = "请求成功"),
            @ApiResponse(code = 20001, message = "请求失败")
    })
    public Result<Brand> findById(@PathVariable("id") Integer id) {
        if (id == null || id == 0) {
            throw new CustomException(StatusCode.IDEMPTY, "id为空错误");//初始化构造器,传参
        }
        Brand brand = brandService.findById(id);
        return new Result<>(true, StatusCode.OK, "查询成功", brand);
    }

    /**
     * @Author: JoinCao
     * @CreateDate: 2020-03-13
     * @Description: 品牌新增
     */
    @PostMapping
    @ApiOperation("添加品牌")
    public Result add(@RequestBody Brand brand) {//@RequestBody将传入的json对象转java对象
        brandService.add(brand);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /**
     * @Author: JoinCao
     * @CreateDate: 2020-03-13
     * @Description: 根据id删除品牌信息
     */
    @DeleteMapping("/{id}")//遵循restful风格
    public Result delById(@PathVariable("id") Integer id) {
        brandService.delById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * @Author: JoinCao
     * @CreateDate: 2020-03-13
     * @Description: 品牌修改
     */
    @PutMapping("/{id}")
    public Result update(@PathVariable("id") Integer id, @RequestBody Brand brand) {//@RequestBody将传入的json对象转java对象
        brand.setId(id);
        brandService.update(brand);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * @Author: JoinCao
     * @CreateDate: 2020-03-13
     * @Description: 品牌列表条件查询
     */
    @GetMapping("/search")
    public Result<List<Brand>> search(@RequestParam Map searchMap) {
        List<Brand> list = brandService.list(searchMap);
        return new Result<>(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * @Author: JoinCao
     * @CreateDate: 2020-03-13
     * @Description: 品牌列表分页查询
     */
    @GetMapping("/search/{page}/{size}")
    public Result findPage(@PathVariable("page") int page, @PathVariable("size") int size) {
        Page<Brand> brandPage = brandService.findPage(page, size);
        return new Result(true, StatusCode.OK, "查询成功",
                new PageResult(brandPage.getTotal(), brandPage.getResult()));
    }

    /**
     * @Author: JoinCao
     * @CreateDate: 2020-03-14
     * @Description: 品牌列表分页+条件查询
     */
    @GetMapping("/searchPage/{page}/{size}")
    @ApiOperation(value = "根据id查询品牌")//一般不写tags
    @ApiImplicitParams({//传参文档描述
            @ApiImplicitParam(name = "page", value = "当前页码", required = true, dataType = "Integer", paramType = "path"),
            @ApiImplicitParam(name = "size", value = "每页显示条数", required = true, dataType = "Integer", paramType = "path"),
            @ApiImplicitParam(name = "name", value = "用于品牌名称", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "letter", value = "用于品牌名称首字母", required = true, dataType = "String", paramType = "path"),
    })
    public Result findPage(@ApiParam(hidden = true) @RequestParam Map searchMap, @PathVariable("page") int page, @PathVariable("size") int size) {
        Page page1 = brandService.findPage(searchMap, page, size);
        return new Result(true, StatusCode.OK, "查询成功",
                new PageResult(page1.getTotal(), page1.getResult()));
    }
}
